<?php
class BaseManageController extends Controller{
	public function init(){
		parent::init();
		if($this->module != null){
			$this->layout = $this->module->layout;
		}
	}
	
	protected function init_pictures($sub_path, &$model, $conf=array()){
		$conf = array_merge(array(
			'save_path' 		=> true, //是否保存路径
			'has_main_image' 	=> true, //是否有主图片
		), $conf);
		if($conf['has_main_image']){
			//有主图片
			$main_picture = $_POST['main_picture'];
			$main_picture = CommonHelper::get_picture_name($main_picture);
		}
		if(isset($_POST['pictures'])){
			//获取更多图片
			$pictures = $_POST['pictures'];
			$path = IMAGE_PATH . $sub_path;
			CommonHelper::ymkdirs(ROOT_PATH . $path);
			foreach ($pictures as $index => $p){
				if(preg_match('/.*\/uploads\/temp\/(.*)/', $p, $matchs)){
					$img_name = $matchs[1];
// 					$file_ext = strtolower(strrchr($img_name , '.'));
					
					//重命名
// 					$img_name = CommonHelper::gen_uuid() . $file_ext;
					rename(ROOT_PATH . $p, ROOT_PATH . $path . $img_name);
					$p = $path . $img_name;
				}else{
					if(!$conf['save_path']){
						$img_name = $p;
						$p = $path . $img_name;
					}
				}
				
				list($width, $height, $type) = getimagesize(ROOT_PATH . $p);
				$data = array(
					'path' => $conf['save_path'] ? $p : $img_name,
					'length' => filesize(ROOT_PATH . $p),
					'width'	=> $width,
					'height' => $height,
				);
				if($conf['has_main_image'] && CommonHelper::end_with($p, $main_picture)){
					$main_picture = $p;
					$data['main'] = true;
				}
				$pictures[$index] = $data;
			}
			$model['pictures'] = $pictures;
			if($conf['has_main_image']){
				$model['main_picture'] = $main_picture;
			}
		}
	}
	
	protected function beforeAction($action){
		if(!parent::beforeAction($action)){
			return false;
		}
		
		$mod = $this->module !== null ? $this->module->id . '@' : '';
		
		$contr_arr = explode('/', $this->id);
		$contr_arr[sizeof($contr_arr) - 1] = ucfirst($contr_arr[sizeof($contr_arr) - 1]);
		$controller = implode('.', $contr_arr);
		
		$controller = str_replace('/', '.', $this->id);
		if(sizeof($contr_arr) == 1){
			$controller = ucfirst($controller);
		}
		$access = $mod . $controller . ucfirst($this->action->id);
		
		$srbac = Yii::getPathOfAlias('srbac');
		$allow_access = require $srbac . '/components/allowed.php';
		if (in_array($access, $allow_access)){
			return true;
		}
		
		if (Yii::app()->user->isGuest) {
			Yii::app()->user->loginRequired();
		}else{
			if(Yii::app()->user->v($access))return true;
			$mod = $this->module !== null ? $this->module->id : "";
			$access = $mod . ucfirst($this->id) . ucfirst($this->action->id);
			throw new CHttpException(403, '权限不足：/'. $mod . '/' . $this->id . '/' . $this->action->id);
		}
	}
}
?>